﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Windstamp.Utility.BookDownload.Common {
    /// <summary>
    /// 后台线程助手类
    /// </summary>
    public class BackgroundWorkerHelper {
        /// <summary>
        /// 后台线程实例
        /// </summary>
        public BackgroundWorker Worker { get; private set; }
        
        /// <summary>
        /// 后台线程助手
        /// </summary>
        public BackgroundWorkerHelper() {
            Worker = new BackgroundWorker();
            Worker.WorkerReportsProgress = true;
            Worker.WorkerSupportsCancellation = true;
        }

        /// <summary>
        /// 开始执行后台线程操作
        /// </summary>
        /// <param name="handler">将要在后台线程执行的操作</param>
        /// <returns></returns>
        public BackgroundWorkerHelper RunWorkerAsync(Action<object, DoWorkEventArgs> handler) {
            Worker.DoWork += new DoWorkEventHandler(handler);
            Worker.RunWorkerAsync();
            return this;
        }
    }
}
